/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.editura.oxygen.severin.imageviewer.features;

import de.editura.common.swing.imageviewer.ImageViewerPanel;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.swing.JComboBox;
import ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace;

/**
 * 
 * @author kschmidtner
 */
public class ImageViewerPanelController {

	private ImageViewerPanel imageViewerPanel = null;
	private StandalonePluginWorkspace pluginWorkspaceAccess = null;
	private JComboBox imagePathCB = null;

	public void refreshImageViewer(String selection) {

		String imageBaseDir = (String) imagePathCB.getSelectedItem();

		if (imageBaseDir.startsWith("http://") | imageBaseDir.startsWith("https://")) {
			URL imageUrl = null;
			URL imageBaseUrl = null;

			try {
				imageBaseUrl = new URL(imageBaseDir);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}

			if (selection.toLowerCase().endsWith(".png") | selection.toLowerCase().endsWith(".jpg")) {
				try {
					imageUrl = new URL(imageBaseUrl, selection);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}

				try {
					imageViewerPanel.getImageCanvas().refreshImgFile(imageUrl);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				imageUrl = testImgSuffixes(imageBaseUrl, selection);
				if (imageUrl != null) {
					imageViewerPanel.getImageCanvas().refreshImgFile(imageUrl);
				} else {
					pluginWorkspaceAccess
							.showInformationMessage("Tests mit Suffix-Ergänzung war nicht erfolgreich. \n Das Bild gibt es nicht: \n"
									+ imageUrl);
				}
			}

		} else {
			File testFile = new File(imageBaseDir, selection);
			// pluginWorkspaceAccess.showInformationMessage("" +
			// testFile.getAbsolutePath() + " - " + testFile.exists());

			File testf = null;
			if (selection.toLowerCase().endsWith(".png") | selection.toLowerCase().endsWith(".jpg")) {
				testf = new File(imageBaseDir, selection);
				if (testf.exists()) {
					imageViewerPanel.getImageCanvas().refreshImgFile(testf);
				} else {
					pluginWorkspaceAccess.showInformationMessage("Suffix ist angegeben. \n Das Bild gibt es nicht: \n"
							+ testf.getAbsolutePath());
				}
			} else {
				testf = testImgSuffixes(imageBaseDir, selection);
				if (testf != null) {
					imageViewerPanel.getImageCanvas().refreshImgFile(testf);
				} else {
					pluginWorkspaceAccess
							.showInformationMessage("Tests mit Suffix-Ergänzung war nicht erfolgreich. \n Das Bild gibt es nicht: \n"
									+ testf.getAbsolutePath());
				}
			}
		}
	}

	private File testImgSuffixes(String imageBaseDir, String selection) {
		String[] suffixes = { "jpg", "JPG", "png", "PNG" };
		File testf = null;
		for (String suffix : suffixes) {
			testf = new File(imageBaseDir, selection + "." + suffix);
			if (testf.exists()) {
				return testf;
			}
		}
		pluginWorkspaceAccess
				.showInformationMessage("Tests mit Suffix-Ergänzung war nicht erfolgreich. \n Das Bild gibt es nicht: \n"
						+ testf.getAbsolutePath());
		return testf;
	}

	private URL testImgSuffixes(URL imageBaseUrl, String selection) {
		String[] suffixes = { "jpg", "JPG", "png", "PNG" };
		URL imageUrl = null;
		HttpURLConnection huc = null;
		int responseCode = 0;

		for (String suffix : suffixes) {
			try {
				imageUrl = new URL(imageBaseUrl, selection + "." + suffix);
				huc = (HttpURLConnection) imageUrl.openConnection();
				huc.setRequestMethod("HEAD");
				responseCode = huc.getResponseCode();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (responseCode == HttpURLConnection.HTTP_OK) {
				return imageUrl;
			}
		}
		pluginWorkspaceAccess
				.showInformationMessage("Tests mit Suffix-Ergänzung war nicht erfolgreich. \n Das Bild gibt es nicht: \n"
						+ imageUrl.toString());
		return imageUrl;
	}

	public void setImagePathCB(JComboBox imagePathCB) {
		this.imagePathCB = imagePathCB;
	}

	public void setImageViewerPanel(ImageViewerPanel imageViewerPanel) {
		this.imageViewerPanel = imageViewerPanel;
	}

	public void setPluginWorkspaceAccess(StandalonePluginWorkspace pluginWorkspaceAccess) {
		this.pluginWorkspaceAccess = pluginWorkspaceAccess;
	}

}
